package com.beanbean.mvvmtest;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;

import com.beanbean.mvvmtest.adapter.ListAdapter;
import com.beanbean.mvvmtest.databinding.ActivityListBinding;
import com.beanbean.mvvmtest.view.IListView;
import com.beanbean.mvvmtest.viewmodel.ListVM;

public class ListActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, IListView {
    private final static String TAG = "ListActivity";
    private ActivityListBinding binding;
    private ListAdapter mListAdapter;
    private ListVM mListVM;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_list);
        initRecyclerView();
        mListVM = new ListVM(this, mListAdapter);
    }

    private void initRecyclerView() {
        binding.userSr.setOnRefreshListener(this);
        binding.userSr.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light, android.R.color.holo_orange_light);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        binding.userRv.setLayoutManager(layoutManager);
        mListAdapter = new ListAdapter(this);
        binding.userRv.setAdapter(mListAdapter);
    }

    @Override
    public void onRefresh() {
        mListVM.loadRefreshData();
    }

    /**
     * 开始加载
     */
    @Override
    public void loadStart() {
        if (!binding.userSr.isRefreshing()) {
            binding.userSr.setRefreshing(true);
        }
    }

    /**
     * 加载完成
     */
    @Override
    public void loadComplete() {
        binding.userSr.setRefreshing(false);
    }

    /**
     * 加载失败
     *
     * @param message
     */
    @Override
    public void loadFailure(String message) {
        binding.userSr.setRefreshing(false);
    }
}
